const socket_url = ":9092";

const socket = io.connect(socket_url + "?clientId=" + $("#client-id").val(), {transports: ["websocket"]});
//监听服务器连接事件
socket.on("connect", function (data) {
    console.log("connect..." + JSON.stringify(data));
    /**并不是都需要所有查询，懒得单独放到每个js，先放一起**/
    selectResponseFromWcs("weighResp", $("#client-id").val(), 5, true, weighRespCallBack);
    selectResponseFromWcs("connect_error", $("#client-id").val(), 5, false, connectErrorCallBack);
    selectResponseFromWcs("wcsToWmsResponse", $("#client-id").val(), 5, false, wcsToWmsResponseCallBack);
    // selectResponseFromWcs("storageDone", $("#client-id").val(), 3, true, storageDoneCallBack);
    selectResponseFromWcs("inBufferPos", $("#client-id").val(), 1, true, inBufferPosCallBack);
    selectResponseFromWcs("sortWeight", $("#client-id").val(), 2, true, sortWeightCallBack);
    selectResponseFromWcs("leaveBufferPos", $("#client-id").val(), 3, true, leaveBufferPosCallBack);
});

//监听服务器关闭服务事件
socket.on("disconnect", function (data) {
    console.log("socket server disconnect..." + JSON.stringify(data));
});

//重新连接
socket.on("reconnect", function (data) {
    console.log("socket server reconnect..." + JSON.stringify(data));
});

//监听服务器错误
socket.on("connect_error", function (error) {
    console.log("connect_error:" + JSON.stringify(error));
    //统一响应异常处理
    selectResponseFromWcs("connect_error", $("#client-id").val(), 5, true, connectErrorCallBack);
});

function connectErrorCallBack(data, cancel, opt) {
    console.log("connect_error:data:" + JSON.stringify(data) + ";cancel:" + JSON.stringify(cancel) + ";opt:" + JSON.stringify(opt));
    if (!data) {
        return;
    }
    delResponseFromWcs(data, opt);
    BootstrapDialog.alert(data.data.msg);
}

//广播消息
socket.on("broadcast", function (data, n) {
    console.log("broadcast server info:" + JSON.stringify(data));
});

socket.on("weighResp", function (data) {
    //显示确认入库按钮
    //清除页面数据
    //称重完成
    const obj = $.parseJSON(data);
    if (obj.code == 0) {
        //processType->UpOrDownPolicy->process_type
        selectResponseFromWcs("weighResp", $("#client-id").val(), 5, false, weighRespCallBack);
    } else {
        if ($('#message-content').hasClass("message-content")) {
            $('#message-content').text(obj.msg);
            setTimeout("$('#message-content').text('')", 5000);
        } else {
            BootstrapDialog.alert(obj.msg);
        }
    }
});

//入库到位
socket.on("storageDone", function (data) {
    //processType->UpOrDownPolicy->process_type
    selectResponseFromWcs("storageDone", $("#client-id").val(), 3, false, storageDoneCallBack);
});

//wcs指令响应
socket.on("wcsToWmsResponse", function (data) {
    //processType->UpOrDownPolicy->process_type
    selectResponseFromWcs("wcsToWmsResponse", $("#client-id").val(), 3, false, wcsToWmsResponseCallBack);
});

//出库到缓存位
socket.on("inBufferPos", function (data) {
    //processType->UpOrDownPolicy->process_type
    selectResponseFromWcs("inBufferPos", $("#client-id").val(), 1, false, inBufferPosCallBack);
});

//分拣称重
socket.on("sortWeight", function (data) {
    selectResponseFromWcs("sortWeight", $("#client-id").val(), 2, false, sortWeightCallBack);
});

//离开缓存位
socket.on("leaveBufferPos", function (data) {
    selectResponseFromWcs("leaveBufferPos", $("#client-id").val(), 2, false, leaveBufferPosCallBack);
});

//称重页面响应
socket.on("electronicWeight", function (data) {
    electronicWeightCallBack(data);
});

//入库缓存位扫码响应
socket.on("pageEntry", function (data) {
    pageEntryCallBack(data);
});